<?php
class Helper_Pager {
    var $records, $pagesize, $max, $page, $NumCount, $Qstring, $full_qstring, $compart_param, $ajax, $context, $cls_current;
    var $firstBtn, $prevBtn, $nextBtn, $lastBtn, $spacer, $jumpbox;
    function Helper_Pager($firstBtn = '首页', $prevBtn = '上一页', $nextBtn = '下一页', $lastBtn = '尾页', $spacer = '') {
        $this -> page = 1;					//当前页页码
        $this -> NumCount = 10;				//显示页码数字个数
        $this -> Qstring = '';				 //？号后面的字符串
        $this -> compart_param = 'page';	 //页码字符
        $this -> hide_context = false;	    //是否隐藏上下页按钮
        $this -> ajax = false;		        //是否调用JS函数分页
        $this -> cls_current = '';	         //当前页CSS
        $this -> firstBtn = $firstBtn;
        $this -> prevBtn = $prevBtn;
        $this -> nextBtn = $nextBtn;
        $this -> lastBtn = $lastBtn;
        $this -> jumpbox = true;            //是否显示jumpbox
        $this -> full_qstring = false;      //是否全地址模式
        $this -> pagesize = 10;             //每页显示
    }

    function nav_print() {
        $output = '';
        if($this -> records > 0) {
            if($this -> records % $this -> pagesize == 0) {
                $this -> max = $this -> records / $this -> pagesize;
            }
            else {
                $this -> max = intval($this -> records / $this -> pagesize) + 1;
            }
        }
//        echo $this -> max;
        if(!$this->page) $this->page = 1;
        $pc = 0;			//显示当前页前后数字的个数
        $starpage = 1;
        $endpage = $this -> NumCount;
        $pc = intval($endpage / 2);

        if($this -> page > $pc) {
            //           echo $this -> page;
            $starpage = $this->page-$pc;

            if($endpage % 2 == 0) {
                $endpage = intval($this -> page) + intval($pc) + 1;
            }
            else {
                $endpage = intval($this -> page) + intval($pc);
            }
        }
        if($this->NumCount == 0) $endpage = 0;

        if($this -> page > $this -> max) $this -> page = $this -> max;

        $output = '<span><a class="records">' .$this -> records. '条</a></span>';

        //如果当前页>1
        if($this -> page > 1 && $this -> hide_context == false) {
            if($this -> ajax == false) {
                if($this-> full_qstring) {
                    $output .= '<span><a value="1" class="firstBtn" href="' .preg_replace('/p__/i', 1, $this ->Qstring). '">'  .$this -> firstBtn. '</a></span>';
                    $output .= '<span><a value="' .($this -> page-1). '" class="prevBtn" href="' .preg_replace('/p__/i', $this -> page - 1, $this ->Qstring). '">' .$this -> prevBtn. '</a></span>';
                }
                else {
                    $output .= '<span><a value="1" class="firstBtn" href="?' .$this -> compart_param. '=1&' .$this -> Qstring. '">'  .$this -> firstBtn. '</a></span>';
                    $output .= '<span><a value="' .($this -> page-1). '" class="prevBtn" href="?' .$this -> compart_param. '=' .($this -> page-1). '&' .$this -> Qstring. '">' .$this -> prevBtn. '</a></span>';
                }
            }
            else {
                $output .= '<span><a value="1" class="firstBtn" href="' .$this -> compart_param. '=1&' .$this -> Qstring. '" onclick="' .preg_replace('/p__/i', 1, $this ->Qstring). 'return false;">' .$this -> firstBtn. '</a></span>';
                $output .= '<span><a value="' .($this -> page-1). '" class="prevBtn" href="[' .preg_replace('/p__/i', $this -> page - 1, $this ->Qstring). ']" onclick="' .preg_replace('/p__/i', $this -> page - 1, $this ->Qstring). 'return false;">' .$this -> prevBtn. '</a></span>';
            }
            //		output .= '<span><a class="number cls_curpage" value="1" href="?'+$this -> compart_param+'=1&'+$this -> Qstring+'">1...</a></span>';
        }

        //当前页>=1 且 当前页<=最大页
        for($i = $starpage; $i <= $endpage; $i++) {
            if($i > $this -> max) { break; }
            if($this-> full_qstring) {
                $href= preg_replace('/p__/i', $i, $this -> Qstring);
                //                $href = preg_replace('/p__/i', $i, $this -> Qstring);
            }else {
                $href = '?' .$this -> compart_param. '=' .$i. '&' .$this -> Qstring;
            }

            if($i == $this -> page) {	//当前页
                if($this -> ajax == false) {
                    $output .= '<span><a class="number cls_curpage" value="' .$i. '" href="' .$href. '">' .$i. '</a></span>' .$this -> spacer;
                }
                else {
                    $output .= '<span><a class="number cls_curpage" value="' .$i. '" href="' .$href. '" onclick="' .preg_replace('/p__/i', $i, $this ->Qstring). 'return false;">' .$i. '</a></span>' .$this -> spacer;
                }
            }
            else {
                if($this -> ajax == false) {
                    $output .= '<span><a class="number" value="' .$i. '" href="' .$href. '">' .$i. '</a></span>' .$this -> spacer;
                }
                else {
                    $output .= '<span><a class="number" value="' .$i. '" href="' .$href. '" onclick="' .preg_replace('/p__/i', $i, $this ->Qstring). 'return false;">' .$i. '</a></span>' .$this -> spacer;
                }
            }
        }

        if($this -> page < $this -> max  && $this -> hide_context == false) {
            if($this -> ajax == false) {

                if($this-> full_qstring) {
                    $href = preg_replace('/p__/i', $this -> page+1, $this ->Qstring);
                    $href_max = preg_replace('/p__/i', $this -> max, $this ->Qstring);
                }else {
                    $href = '?' .$this -> compart_param. '=' .($this -> page + 1). '&' .$this -> Qstring;
                }

                $output .= '<span><a value="' .($this -> page + 1). '" class="nextBtn" href="' .$href. '">' .$this -> nextBtn. '</a></span>';
                $output .= '<span><a value="' .$this -> max. '" class="lastBtn" href="' .$href_max. '">' .$this -> lastBtn. '</a></span>';
            }else {
                $output .= '<span><a class="nextBtn" href="' .$href. '" onclick="' .preg_replace('/p__/i', $this -> page + 1, $this ->Qstring). 'return false;">' .$this -> nextBtn. '</a></span>';
                $output .= '<span><a class="lastBtn" href="' .$href. '" onclick="' .preg_replace('/p__/i', $this -> max, $this ->Qstring). 'return false;">' .$this -> lastBtn. '</a></span>';
            }
        }
        $output .= '<span><a class="records">' .$this -> max. '页</a></span>';

        if($this -> jumpbox) {
            if($this -> ajax) {
                $output .= '<span><input class="jump_box" type="" id="my_js_pager_jumper" size="2" onclick="this.select();" onchange="' .preg_replace('/p__/i', 'this.value', $this ->Qstring). '" /></span>';
            }
            else {
                $output .= '<span><input class="jump_box" type="" size="2" onchange="location=document.getElementById(\'LZW_pager_jump_url\').value.replace(/p__/i, this.value);" /></span>';
            }
        }
        $output .= '<input type="hidden" name="LZW_pager_pageid" id="LZW_pager_pageid" value="' .$this -> page. '"/><input type="hidden" value="' .$this->Qstring. '" id="LZW_pager_jump_url" />';

        if($this -> max == 1 || $this -> max == 0){
            return '';
        }
        return($output);
    }
}
?>